package com.zadtek.sharkRider.activity;

import java.util.ArrayList;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.zadtek.sharkRider.R;
import com.zadtek.sharkRider.activity.adapter.UserDetailsAdapter;
import com.zadtek.sharkRider.activity.common.Common;
import com.zadtek.sharkRider.activity.common.Const;
import com.zadtek.sharkRider.activity.data.UserDetailsData;
import com.zadtek.sharkRider.frame.JsonProcessHelper;
import com.zadtek.sharkRider.views.PullDownView;
import com.zadtek.sharkRider.views.PullDownView.OnPullDownListener;
import com.zadtek.sharkRider.views.ScrollOverListView;

/**
 * 余额明细
 * 
 * @author wwl
 * 
 */
public class UserDetail extends BaseActivity implements OnClickListener {
	ImageView backid;
	TextView topview; // 标题
	TextView tv01, tv02;// 收支明细、提现明细
			// ListView listview;
	TextView showtv;
	private ArrayList<UserDetailsData> detailList = new ArrayList<UserDetailsData>();
	private ArrayList<UserDetailsData> more_detailList = new ArrayList<UserDetailsData>();
	private Handler mHandler;
	private SharedPreferences sp;
	String uid;
	private UserDetailsAdapter adapter;
	private PullDownView pullDownView;
	private ScrollOverListView listView;
	private int refs = 1; // 页码
	private String page = "1";
	private int pagecount = 0; // 中页数
	private View mHeaderView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.money_detail);

		Const.DETAIL_INDEX = 0;
		sp = getSharedPreferences(Const.PREF_NAME, Context.MODE_PRIVATE);
		uid = sp.getString("uid", "");

		backid = (ImageView) findViewById(R.id.backid);
		topview = (TextView) findViewById(R.id.topview);
		tv01 = (TextView) findViewById(R.id.tv01);
		tv02 = (TextView) findViewById(R.id.tv02);
		tv01.setOnClickListener(this);
		tv02.setOnClickListener(this);
		topview.setText("余额明细");
		backid.setOnClickListener(this);
		// listview = (ListView) findViewById(R.id.listview);
		pullDownView = (PullDownView) findViewById(R.id.pulldownview);
		pullDownView.enableAutoFetchMore(true, 0);
		listView = pullDownView.getListView();
		listView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		showtv = (TextView) findViewById(R.id.showtv);
		mHeaderView =findViewById(R.id.mHeaderView);
		// 获取信息
		getinitArray();

		pullDownView.setOnPullDownListener(new OnPullDownListener() {

			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				initArrays(new Handler() {
					@Override
					public void handleMessage(Message msg) {
						if (!"emp".equals(msg.obj.toString())) {
							showtv.setVisibility(View.GONE);

							refs = 1;
							detailList = (ArrayList<UserDetailsData>) msg.obj;
							if (detailList.get(0).getStatus().equals("1")) {
								adapter = new UserDetailsAdapter(
										UserDetail.this, detailList,
										R.layout.userdetails_item);
								listView.setAdapter(adapter);
								adapter.notifyDataSetChanged();
								pullDownView.setVisibility(View.VISIBLE);
							} else {
								// 没有订单
								showtv.setVisibility(View.VISIBLE);
								pullDownView.setVisibility(View.GONE);
							}
						} else {
							showtv.setVisibility(View.VISIBLE);
							pullDownView.setVisibility(View.GONE);
							if (detailList == null) {
								Common.DisplayToast(UserDetail.this,
										"通信失败,请检查网络!", 1);
							}
						}
						pullDownView.notifyDidRefresh(true);
						if (pagecount <= refs) {
							pullDownView.notifyDidLoadMore(true);
						} else {
							pullDownView.notifyDidLoadMore(false);
						}
					}
				});
			}

			@Override
			public void onLoadMore() {
				// TODO Auto-generated method stub
				getNewString(new Handler() {
					@Override
					public void handleMessage(Message msg) {

						if (!"emp".equals(msg.obj.toString())) {
							more_detailList = (ArrayList<UserDetailsData>) msg.obj;
							int size = more_detailList.size();
							for (int i = 0; i < size; i++) {
								detailList
										.add((UserDetailsData) more_detailList
												.get(i));
							}

							adapter = new UserDetailsAdapter(UserDetail.this,
									detailList, R.layout.userdetails_item);
							listView.setAdapter(adapter);
							adapter.notifyDataSetChanged();

						} else {
							Common.DisplayToast(UserDetail.this, "通信失败,请检查网络!",
									1);
						}
						if (pagecount <= refs) {
							pullDownView.notifyDidLoadMore(true);
						} else {
							pullDownView.notifyDidLoadMore(false);
						}
					}
				});
			}
		});
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.backid:
			finish();
			break;
		case R.id.tv01:
			Const.DETAIL_INDEX = 0;
			tv01.setTextColor(getResources().getColor(R.color.single_money));
			tv02.setTextColor(getResources().getColor(R.color.detail_tv));
			tv01.setBackgroundResource(R.drawable.ordermenu_lasth);
			tv02.setBackgroundResource(R.drawable.ordermenu_last);
			// 获取信息
			if (Common.isNetworkConnected(UserDetail.this)) {
				mHeaderView.setVisibility(View.VISIBLE);
				refs = 1;
				getinitArray();
			} else {
				Common.DisplayToast(UserDetail.this, "通信失败,请检查网络!", 2);
			}
			break;

		case R.id.tv02:
			Const.DETAIL_INDEX = 1;
			tv01.setTextColor(getResources().getColor(R.color.detail_tv));
			tv02.setTextColor(getResources().getColor(R.color.single_money));
			tv01.setBackgroundResource(R.drawable.ordermenu_last);
			tv02.setBackgroundResource(R.drawable.ordermenu_lasth);
			// 获取信息
			if (Common.isNetworkConnected(UserDetail.this)) {
				mHeaderView.setVisibility(View.VISIBLE);
				refs = 1;
				getinitArray();
			} else {
				Common.DisplayToast(UserDetail.this, "通信失败,请检查网络!", 2);
			}
			break;

		default:
			break;
		}
	}

	// 获取初始化数据
	public void getinitArray() {
		
		initArrays(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				mHeaderView.setVisibility(View.GONE);
				if (!"emp".equals(msg.obj.toString())) {
					showtv.setVisibility(View.GONE);
					detailList = new ArrayList<UserDetailsData>();
					detailList = (ArrayList<UserDetailsData>) msg.obj;

					// adapter.notifyDataSetChanged();

					pullDownView.notifyDidDataLoad(false);
					if (detailList.size() > 0
							&& detailList.get(0).getStatus().equals("1")) {
						adapter = new UserDetailsAdapter(UserDetail.this,
								detailList, R.layout.userdetails_item);
						listView.setAdapter(adapter);
						showtv.setVisibility(View.GONE);
						pullDownView.setVisibility(View.VISIBLE);
						// 计算出总页数
						int count = Integer.parseInt(detailList.get(0)
								.getTotal());
						int pagenum = Integer.parseInt(detailList.get(0)
								.getNumber());
						pagecount = count / pagenum
								+ (count % pagenum > 0 ? 1 : 0);
						if (pagecount <= 1) {
							pullDownView.notifyDidLoadMore(true);
						}
					} else {
						// 没有订单
						showtv.setVisibility(View.VISIBLE);
						pullDownView.setVisibility(View.GONE);
					}

				} else {
					showtv.setVisibility(View.VISIBLE);
					pullDownView.setVisibility(View.GONE);

					pullDownView.notifyDidDataLoad(false);
					if (detailList == null) {
						Common.DisplayToast(UserDetail.this, "通信失败,请检查网络!", 1);
					} else {
						pullDownView.notifyDidLoadMore(true);
					}
				}
			}
		});
	}

	// 加载更多
	private void getNewString(final Handler handler) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(2000);
					refs++;

					int threadcount = 0, pagenum = 0;
					int fy = 0;
					threadcount = Integer
							.parseInt(detailList.get(0).getTotal());
					pagenum = Integer.parseInt(detailList.get(0).getNumber());
					fy = threadcount / pagenum
							+ (threadcount % pagenum > 0 ? 1 : 0);
					pagecount = fy;
					if (fy >= refs) {
						// 从缓存中获取用户信息
						sp = UserDetail.this.getSharedPreferences(
								Const.PREF_NAME, Context.MODE_PRIVATE);
						String uid = sp.getString("uid", "");
						if (Const.DETAIL_INDEX == 0) {
							more_detailList = JsonProcessHelper
									.jsonProcess_getEmpAccountlog(
											"get_emp_accountlog", uid, refs
													+ "");
						} else if (Const.DETAIL_INDEX == 1) {
							more_detailList = JsonProcessHelper
									.jsonProcess_getEmpAccountlog(
											"get_emp_depositlog", uid, refs
													+ "");
						}
						if (more_detailList != null) {
							handler.obtainMessage(0, more_detailList)
									.sendToTarget();
						} else {
							refs--;
							handler.obtainMessage(0, "emp").sendToTarget();
						}
					} else {
						handler.obtainMessage(0, "emp").sendToTarget();
					}
				} catch (Exception e) {
					Thread.interrupted();
					e.printStackTrace();
				}
			}
		}).start();
	}

	// 线程加载数据
	private void initArrays(final Handler handler) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					// 从缓存中获取用户信息
					sp = UserDetail.this.getSharedPreferences(Const.PREF_NAME,
							Context.MODE_PRIVATE);
					String uid = sp.getString("uid", "");
					if (Const.DETAIL_INDEX == 0) {
						detailList = new ArrayList<UserDetailsData>();
						detailList = JsonProcessHelper
								.jsonProcess_getEmpAccountlog(
										"get_emp_accountlog", uid, page);
					} else if (Const.DETAIL_INDEX == 1) {
						detailList = new ArrayList<UserDetailsData>();
						detailList = JsonProcessHelper
								.jsonProcess_getEmpAccountlog(
										"get_emp_depositlog", uid, page);
					}
					if (detailList != null) {
						handler.obtainMessage(0, detailList).sendToTarget();

					} else {
						handler.obtainMessage(0, "emp").sendToTarget();
					}

				} catch (Exception e) {
					Thread.interrupted();
					e.printStackTrace();
				}
			}
		}).start();
	}

}